unit syncform;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, BCButton, BCLabel, Forms, Controls, Graphics,
  Dialogs, ExtCtrls, StdCtrls, nkTitleBar;

type

  { TfrmSync }

  TfrmSync = class(TForm)
    BCButton1: TBCButton;
    BCLabel1: TBCLabel;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    CheckBox6: TCheckBox;
    GroupBox2: TGroupBox;
    Image2: TImage;
    Label1: TLabel;
    Label2: TLabel;
    Memo1: TMemo;
    nkTitleBar1: TnkTitleBar;
    Panel2: TPanel;
    Panel8: TPanel;
    Panel9: TPanel;
    procedure BCButton1Click(Sender: TObject);
    procedure BCLabel1Click(Sender: TObject);
    procedure Image2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Image2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer
      );
    procedure Image2MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    Moving:boolean;
    XPos,YPos:integer;
  public
    procedure Log(AText:string);
    procedure DoSync(ATable:string);
  end;

var
  frmSync: TfrmSync;

implementation

uses
  datamodule;
{$R *.frm}

{ TfrmSync }

procedure TfrmSync.BCButton1Click(Sender: TObject);
begin
  if dm.Platform='公共库' then
  begin
    MessageDlg('正处于公共库中，无需同步。',mtWarning,[mbOK],0);
    Exit;
  end;
  //Exit;
  BCButton1.Enabled:=False;
  Log('====================');
  Log('开始同步，请耐心等待完成');
  Log('====================');
  if CheckBox1.Checked then
  begin
    Log('正在同步死亡人员名单……');
    DoSync('siwang');
  end;
  if CheckBox2.Checked then
  begin
    Log('正在同步职工参保名单……');
    DoSync('zhigong');
  end;
  if CheckBox3.Checked then
  begin
    Log('正在同步居民参保名单……');
    DoSync('jumin');
  end;
  if CheckBox4.Checked then
  begin
    Log('正在同步职工慢性病编码……');
    DoSync('zhigongmanbingbianma');
  end;
  if CheckBox5.Checked then
  begin
    Log('正在同步居民慢性病编码……');
    DoSync('manbingbianma');
  end;
  if Checkbox6.Checked then
  begin
    Log('正在同步定点医疗机构编码与级别对应表……');
    DoSync('yiyuanbianma');
  end;
  Log('====================');
  Log('完成同步');
  Log('====================');
  BCButton1.Enabled:=True;
end;

procedure TfrmSync.BCLabel1Click(Sender: TObject);
begin
  ModalResult:=mrCancel;
end;

procedure TfrmSync.Image2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if mbLeft = Button then
  begin
    Self.Moving:=True;
    Self.YPos := Y;
    Self.XPos := X;
  end;
end;

procedure TfrmSync.Image2MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if not (csDesigning in Self.ComponentState) then
  begin
    if Self.Moving then
    begin
      Self.Top:=Self.Top+Y-Self.YPos;
      Self.Left:=Self.Left+X-Self.XPos;
      if Self.Top<0 then
        Self.Top:=0;
    end;
  end;
end;

procedure TfrmSync.Image2MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Moving:=False;
end;

procedure TfrmSync.Log(AText: string);
begin
  Memo1.Lines.Add(AText);
  Application.ProcessMessages;
end;

procedure TfrmSync.DoSync(ATable: string);
var
  mWorkDB:string;
begin
  //清除工作库中对应表数据
  mWorkDb:=dm.connWork.Database;
  with dm.pubqryWork do
  begin
    Close;
    Sql.Clear;
    Sql.Add('delete from '+ mWorkDB+'.'+ATable);
    ExecSQL;
  end;
  with dm.pubqry do
  begin
    Close;
    Sql.Clear;
    Sql.Add('insert into '+mWorkDb+'.'+ATable+' select * from neikong.'+ATable);
    ExecSQL;
  end;
end;

end.

